package com.ruoyi.AI.controller;

import cn.hutool.json.JSONUtil;
import com.ruoyi.AI.domain.ChatReq;
import com.ruoyi.AI.domain.MessagesLogs;
import com.ruoyi.AI.entity.GPTData;
import com.ruoyi.AI.service.ChartGPTService;
import com.ruoyi.AI.service.impl.MessagesLogsServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DistanceUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "gpt统一接口",description = "gpt请求统一接口")
@RestController
@RequestMapping("/gpt")
public class GPTTestController {
    @Autowired
    ChartGPTService chartGPTService;

    @Autowired
    MessagesLogsServiceImpl messagesLogsService;

    @Autowired
    private ServerConfig serverConfig;

    @Value("${ruoyi.url}")
    private String requrl;

    @ApiOperation("发送内容")
    @PostMapping ("/sendFlux")
    @ResponseBody
    public AjaxResult sendFlux(@RequestBody ChatReq req) {
        AjaxResult ajax = AjaxResult.success("");
        try
        {
            String msg= chartGPTService.sendFluxCoze(req.getUid(),req.getText());

            ajax = AjaxResult.success(msg);
        }catch (Exception e){
            ajax = AjaxResult.error(e.getMessage());
        }
        return ajax;
    }

    @ApiOperation("获取聊天记录")
    @GetMapping("/getmessages")
    @ResponseBody
    public AjaxResult getmessages() {
        AjaxResult ajax = AjaxResult.success("");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MessagesLogs messagesLogsReq=new MessagesLogs();
        messagesLogsReq.setUserId(loginUser.getUserId().toString());
        messagesLogsReq.setDelFlag(0L);
        val messagesLogs = messagesLogsService.selectMessagesLogsList(messagesLogsReq);
        ajax = AjaxResult.success(messagesLogs);
        return ajax;
    }




}
